//
//  MeViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/17.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "MeViewController.h"
#import "SettingViewController.h"
#import "UIView+Extend.h"
#import "MeMemuView.h"
#import "LogOutView.h"
#import "LoginView.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "OSSHelper.h"
@interface MeViewController ()
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)UIView *buttonView;
@property(nonatomic,strong)MeMemuView *mev;
@property(nonatomic,strong )NSMutableArray *messageCout;

@end

@implementation MeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(USERID.length) {
        [self loadData];
       }
}

-(void)loadData{
    //2.26我的页面（经判断用户已经登录）
    self.messageCout = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"].mutableCopy;
    __weak typeof(self) weakSelf = self;
    NSDictionary *paraDic = @{@"userId":USERID};
    [Tools AFNetworkingRequestTypePostWithUrl:MYHOMEPAGE paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"我的页面--%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dic  = resultDic[@"data"];
            if([dic isKindOfClass:[NSNull class]]) return ;
            //购物车中商品的数量)
            [weakSelf.messageCout replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%@",dic[@"numOfCart"]]];
           //未付款商品数量)
            [weakSelf.messageCout replaceObjectAtIndex:4 withObject:[NSString stringWithFormat:@"%@",dic[@"numUnpaid"]]];
            //未发货订单数量
            [weakSelf.messageCout replaceObjectAtIndex:7 withObject:[NSString stringWithFormat:@"%@",dic[@"numUO"]]];
            //待收货数量
            [weakSelf.messageCout replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%@",dic[@"numUnReceiving"]]];
            //待评价数量
            [weakSelf.messageCout replaceObjectAtIndex:5 withObject:[NSString stringWithFormat:@"%@",dic[@"numUnEvaluate"]]];
            //已完成的数量
            [weakSelf.messageCout replaceObjectAtIndex:8 withObject:[NSString stringWithFormat:@"%@",dic[@"numCompelete"]]];
            weakSelf.mev.messageCout = weakSelf.messageCout;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changePhoto" object:dic];
            }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.vcName = @"我的";
    //设置按钮
    [self setButton];
    //头视图
    [self initHeadView];
    //按钮九宫格
    [self initButtonView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSuccess) name:@"loginSuccess" object:nil];
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logOutSuccess) name:@"logOutSuccess" object:nil];
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//接受到登录成功的通知后加载数据
-(void)loginSuccess{
    [self.headView removeFromSuperview];
    [self initHeadView];
}
-(void)logOutSuccess{
    [self.headView removeFromSuperview];
    [self initHeadView];
}

-(void)initHeadView{
    self.headView= [[UIView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Width*.44)];
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:self.headView.bounds];
    //判断是否登录
    if([AppDelegate appDealegate].uid.length){//已登陆
          imageView1.image = [UIImage imageNamed:@"wode_bg_sel"];
           [self.headView addSubview:imageView1];
        //遮罩视图
        UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Width*.44)];
        imageView2.image = [UIImage imageNamed:@"wode_zhezhao"];
        [self.headView addSubview:imageView2];
        //登录后的页面
        LoginView  *loginView = [[NSBundle mainBundle]loadNibNamed:@"LoginView" owner:self options:nil][0];
        loginView.origin = CGPointMake(0, 0);
        [self.headView addSubview:loginView];
    }else{//未登录
        imageView1.image = [UIImage imageNamed:@"bg_weidenglu"];
        [self.headView addSubview:imageView1];
        LogOutView  *logOutView = [[NSBundle mainBundle]loadNibNamed:@"LogOutView" owner:self options:nil][0];
        logOutView.centerX = self.headView.centerX;
        logOutView.centerY = self.headView.centerY-50;
        [self.headView addSubview:logOutView];
    }
    [self.view addSubview:self.headView];
}

-(void)initButtonView{
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, self.headView.bottom, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight-49-self.headView.height)];
    MeMemuView *mev = [[MeMemuView alloc]initWithFrame:self.buttonView.bounds];
    self.mev= mev;
    [self.buttonView addSubview:mev];
    [self.view addSubview:self.buttonView];
}

-(void)setButton{
    UIButton *locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    locationButton.frame=CGRectMake(0, 0, 30, 40);
    [locationButton setImage:[UIImage imageNamed:@"icon_shezhi_nor"] forState:UIControlStateNormal];
    [locationButton addTarget:self action:@selector(setAction) forControlEvents:UIControlEventTouchUpInside];
    self.rightItem = locationButton;
}

- (void)setAction{
    
    if(![AppDelegate appDealegate].uid.length){
        LoginViewController *login = [[LoginViewController alloc]init];
        login.isModel = YES;
        BaseNavigationViewController *baseNavi = [[BaseNavigationViewController alloc]initWithRootViewController:login];
        [self  presentViewController:baseNavi animated:YES completion:nil];
        return;
    }

    SettingViewController  *settingVc = [[SettingViewController alloc]init];
    BaseNavigationViewController *baseNavi  = [[BaseNavigationViewController alloc]initWithRootViewController:settingVc];
    settingVc.isModel = YES;
    [self presentViewController:baseNavi animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
